<template>
    <div>
        <Modal v-model="isShowModal" :title="title" @on-cancel="hide('formValidate')" :mask-closable="false">

            <Form ref="formValidate" :model="params" :rules="ruleValidate" label-position="top">
                <FormItem label="配置值：" prop="configValue">
                    <Input v-model.trim="params.configValue" placeholder="请输入配置值"></Input>
                </FormItem>
                <FormItem label="配置描述：" prop="remark">
                    <Input v-model.trim="params.remark" placeholder="请输入配置描述"></Input>
                </FormItem>

                <FormItem label="虚拟户二维码：" prop="configKey" v-if="params.configKey == 'AdminVirtualAccountImg'">
                    <div class="j-img-echo">
                        <template v-if="!params.configValue">
                            <Icon type="md-images" size="55" color="#c5c8ce" />
                        </template>
                        <template v-else>
                            <img :src="params.configValue">
                        </template>
                    </div>
                    <Upload :action="action" :headers="headers" :show-upload-list="false" name="file" :on-success="uploadSuccess">
                        <Button icon="ios-cloud-upload-outline" style="width:488px">上传虚拟户二维码</Button>
                    </Upload>
                </FormItem>

            </Form>

            <!-- 重写对话框底部按钮 -->
            <div slot="footer">
                <Button @click="hide('formValidate')">取消</Button>
                <Button v-if="type != 1" type="primary" @click="handleSubmit('formValidate')" :loading="loadingBtn">
                    <span v-if="!loadingBtn">确定</span>
                    <span v-else>请稍候...</span>
                </Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    export default {
        name: "config-ope",
        data() {
            return {
                isShowModal: false,
                loadingBtn: false,
                title: '查看信息',
                type: 0,
                action: this.$uploadURL,
                headers: {
                    token: JSON.parse(sessionStorage.getItem('J-TOKEN')).token
                },
                params: {
                    configValue: '',
                    remark: '',
                    configId:'',
                    configKey:'',
                },
                ruleValidate: {
                    configValue: [
                        {required: true, message: '配置值不能为空', trigger: 'change'}
                    ],
                    remark: [
                        {required: true, message: '配置描述不能为空', trigger: 'change'}
                    ]
                }
            }
        },
        methods: {
            // 点击提交验证
            handleSubmit(name) {
                // console.log(this.params);
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        // this.$Message.error('验证通过');
                        /**
                         * type判断
                         * 1.查看 2.新增 3.编辑
                         */
                        switch (this.type) {
                            case 1:
                                // this.title = '查看配置';
                                break;
                            case 2:
                                // 新增配置
                                break;
                            case 3:
                                // 编辑配置
                                this.updateConfig();
                                break;
                            default:
                                this.$Notice.error({
                                    title: '配置管理',
                                    desc: '查看，新增，编辑区分判断报错',
                                    duration: 0
                                });
                                break
                        }
                    } else {
                        this.$Message.error('还有信息没有填写合格哦~');
                    }
                })
            },
            // 显示模态框
            show(type, row) {
                this.isShowModal = true;
                this.type = type;
                /**
                 * type判断
                 * 1.查看 2.新增 3.编辑
                 */
                switch (type) {
                    case 1:
                        this.title = '查看配置';
                        this.findConfigById(row);
                        break;
                    case 2:
                        this.title = '新增配置';
                        break;
                    case 3:
                        this.title = '编辑配置';
                        this.findConfigById(row);
                        break;
                    default:
                        this.$Notice.error({
                            title: '平台配置管理',
                            desc: '查看，新增，编辑模态框报错',
                            duration: 0
                        });
                        break
                }
            },
            // 编辑配置
            updateConfig(){
                console.log(this.params)

                this.$Spin.show();
                this.axios.post('/config/updateConfig', this.params).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        this.$emit('changeData');
                        this.$Message.success('操作成功.');
                        this.hide('formValidate');
                    }
                    if(res.status == 201){
                        this.$Message.error(res.msg);
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '编辑配置接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 查询配置详情
            findConfigById(row){
                this.$Spin.show();
                this.axios.post('/config/findConfigById', {
                    configId: row.configId
                }).then(res => {
                    
                    setTimeout(()=>{
                        this.$Spin.hide();
                    },300);
                    if (res.status == 200) {
                        // this.$Message.success('操作成功.');
                        this.params.configValue = res.data.configValue;
                        this.params.remark = res.data.remark;
                        this.params.configId = res.data.configId;
                        this.params.configKey = res.data.configKey;
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '查看配置详情接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 上传成功
            uploadSuccess(response, file, fileList){
                if (response.status == 200) {
                    this.params.configValue = response.data;
                }
                if(response.status == 201){
                    this.$Message.error(response.msg);
                }
                if (response.status == 500) {
                    this.$Notice.error({
                        title: '服务器内部错误',
                        desc: '上传虚拟账户接口报错',
                        duration: 0
                    });
                }
            },
            // 关闭模态框
            hide(name) {
                this.isShowModal = false;
                this.params.configId = '';
                this.$refs[name].resetFields();
            }
        }
    }
</script>

<style scoped>
    .j-img-echo{
        height: 426px;
    }
</style>